﻿using System;
using System.Collections.Generic;
using System.Text;
using Csla;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using Csla.Data;

namespace buildingLib.fee
{
    public class InvoiceTaskList : BusinessListBase<InvoiceTaskList,InvoiceTicket>
    {
        #region Factory Method
        private InvoiceTaskList() { }
        public static InvoiceTaskList GetInvoiceItems()
        {
            InvoiceTaskList invoiceTaskList = new InvoiceTaskList();
            invoiceTaskList.Fetch("");
            return invoiceTaskList;
        }
        public static InvoiceTaskList GetInvoiceItems(string where)
        {
            InvoiceTaskList invoiceTaskList = new InvoiceTaskList();
            invoiceTaskList.Fetch(where);
            return invoiceTaskList;
        }
           
        #endregion

        #region DataAccess
        public void Fetch(object criteria)
        {
            RaiseListChangedEvents = false;
            this.Clear();
            //查询InvoiceTask表，找到所有Ticket
            string sql = "SELECT [invoice].id FROM [invoice] Left JOIN [invoice_task] ON [invoice].ID = [invoice_task].ticketID WHERE ";
            sql += criteria.ToString();
            sql += " 1=1";
            Database db = SQLDatabase.GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            using (SafeDataReader dr = new SafeDataReader(db.ExecuteReader(cmd)))
            {
                while(dr.Read())
                    this.Add(InvoiceTicket.GetTicket(dr.GetGuid(0)));
            }
            RaiseListChangedEvents = true;
        }
        #endregion
    }
}
